package com.meritdata.cloud.data.standard.table.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.meritdata.cloud.data.base.Page;
import com.meritdata.cloud.data.base.ResultData;
import com.meritdata.cloud.data.standard.table.dao.IDictCommonValueDao;
import com.meritdata.cloud.data.standard.table.dao.IDictModelDao;
import com.meritdata.cloud.data.standard.table.dao.IDictPartStructDao;
import com.meritdata.cloud.data.standard.table.dao.IDictPartValueDao;
import com.meritdata.cloud.data.standard.table.entity.TdDictModel;
import com.meritdata.cloud.data.standard.table.entity.TdDictPartStruct;
import com.meritdata.cloud.data.standard.table.service.IDictModelService;

@Service
public class DictModelService implements IDictModelService {

	@Autowired
	private IDictModelDao dictModelDao;

	@Autowired
	private IDictCommonValueDao dictCommonValueDao;

	@Autowired
	private IDictPartStructDao dictPartStructDao;

	@Autowired
	private IDictPartValueDao dictPartValueDao;

	@Override
	public Page<TdDictModel> page(Long dirId, int pageNum, int pageSize, String name) {
		if (StringUtils.isBlank(name)) {
			return dictModelDao.pageHql("from TdDictModel t where t.dirId = ? order by t.name asc", pageNum, pageSize,
					dirId);
		} else {
			return dictModelDao.pageHql(
					"from TdDictModel t where t.dirId = ? and t.name like ? order by t.name asc", pageNum, pageSize,
					dirId, "%" + name.trim() + "%");
		}
	}

	@Override
	public TdDictModel get(Long id) {
		return dictModelDao.findOne(id);
	}

	@Override
	public ResultData create(String name, String code, String description, Long dirId, String userId) {
		ResultData result = new ResultData();
		if (isExistCode(code, dirId, 0L)) {
			result.setStatus(false);
			result.setMsg("编码已存在");
			return result;
		}

		if (isExistName(name, dirId, 0L)) {
			result.setStatus(false);
			result.setMsg("名称已存在");
			return result;
		}

		TdDictModel dict = new TdDictModel();
		dict.setCode(code);
		dict.setCreateTime(new Date());
		dict.setCreator(userId);
		dict.setDescription(description);
		dict.setDirId(dirId);
		dict.setEnable(0);
		dict.setName(name);
		dictModelDao.save(dict);
		result.setData(dict);
		return result;
	}

	@Override
	public ResultData update(String name, String code, String description, Long id, String userId) {
		ResultData result = new ResultData();
		TdDictModel dict = dictModelDao.findOne(id);
		if (dict == null) {
			result.setStatus(false);
			result.setMsg("字典已不存在");
			return result;
		}

		if (isExistCode(code, dict.getDirId(), id)) {
			result.setStatus(false);
			result.setMsg("编码已存在");
			return result;
		}

		if (isExistName(name, dict.getDirId(), id)) {
			result.setStatus(false);
			result.setMsg("名称已存在");
			return result;
		}

		dict.setCode(code);
		dict.setName(name);
		dict.setDescription(description);
		dict.setReviserId(userId);
		dict.setUpdateTime(new Date());
		dictModelDao.update(dict);
		result.setData(dict);
		return result;
	}

	@Override
	@Transactional
	public ResultData delete(Long id) {
		ResultData result = new ResultData();
		List<TdDictPartStruct> partStructs = dictPartStructDao.findEqualField("dictId", id);
		if (!CollectionUtils.isEmpty(partStructs)) {
			String[] questions = new String[partStructs.size()];
			Object[] partIds = new Object[partStructs.size()];
			int i = 0;
			for (TdDictPartStruct tdDictPartStruct : partStructs) {
				questions[i] = "?";
				partIds[i] = tdDictPartStruct.getId();
				i++;
			}
			dictPartValueDao.executeHql(
					"delete from TdDictPartValue t where t.partId in (" + StringUtils.join(questions, ",") + ")",
					partIds);
		}
		dictPartStructDao.deleteEqualField("dictId", id);
		dictCommonValueDao.deleteEqualField("dictId", id);

		int delete = dictModelDao.delete(id);
		result.setStatus(delete > 0);
		return result;
	}

	@Override
	public ResultData enable(Long id, int status) {
		ResultData result = new ResultData();
		TdDictModel dict = dictModelDao.findOne(id);
		if (dict == null) {
			result.setStatus(false);
			result.setMsg("字典已不存在");
			return result;
		}
		dict.setEnable(status);
		dictModelDao.update(dict);
		result.setData(dict);
		return result;
	}

	private boolean isExistName(String name, Long dirId, Long id) {
		Long countHql = dictModelDao.countHql("from TdDictModel t where t.dirId = ? and t.name = ? and id <> ?", dirId,
				name, id);
		return countHql > 0;
	}

	private boolean isExistCode(String code, Long dirId, Long id) {
		Long countHql = dictModelDao.countHql("from TdDictModel t where t.dirId = ? and t.code = ? and id <> ?", dirId,
				code, id);
		return countHql > 0;
	}

}
